/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package elaborate.h2x.transform.service;

import elaborate.h2x.parser.QNameUtil;
import elaborate.h2x.transform.TransformingModel;
import elaborate.h2x.transform.TransformingNode;
import javax.xml.namespace.QName;
import org.w3c.dom.Attr;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.Text;

/**
 *
 * @author lendle
 */
public class DefaultNodeGeneratorImpl implements NodeGenerator{

    public Node createNode(TransformingModel model, TransformingNode targetNode, String def_exp) {
        def_exp=def_exp.trim();
        if("text()".equals(def_exp)){
            Text text=model.getDocument().createTextNode(targetNode.getOriginalNode().getNodeValue());
            //System.out.println("createNode: "+text);
            return text;
        }else if(def_exp.startsWith("@")){
            //attr node
            QName qName=QNameUtil.string2QName(def_exp);
            Attr attr=(qName.getNamespaceURI()==null)?model.getDocument().createAttribute(qName.getLocalPart()):model.getDocument().createAttributeNS(qName.getNamespaceURI(), qName.getLocalPart());
            return attr;
        }else{
            //element node
            QName qName=QNameUtil.string2QName(def_exp);
            Element element=(qName.getNamespaceURI()==null)?model.getDocument().createElement(qName.getLocalPart()):model.getDocument().createElementNS(qName.getNamespaceURI(), qName.getLocalPart());
            //System.out.println("createNode: "+element);
            return element;
        }
    }

}
